<template>
    <el-dialog
            :title="!dataForm.logId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="调用记录ID，主键" prop="logId">
                        <el-input v-model.number="dataForm.logId" placeholder="调用记录ID，主键"></el-input>
                </el-form-item>
                <el-form-item label="接口ID，关联app_integration_interface表" prop="interfaceId">
                        <el-input v-model.number="dataForm.interfaceId" placeholder="接口ID，关联app_integration_interface表"></el-input>
                </el-form-item>
                <el-form-item label="调用时间" prop="callTime">
                        <el-date-picker
                                v-model="dataForm.callTime"
                                type="datetime"
                                placeholder="调用时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="响应状态码" prop="statusCode">
                        <el-input v-model.number="dataForm.statusCode" placeholder="响应状态码"></el-input>
                </el-form-item>
                <el-form-item label="调用方IP地址" prop="ipAddress">
                        <el-input v-model="dataForm.ipAddress" placeholder="调用方IP地址"></el-input>
                </el-form-item>
                <el-form-item label="调用耗时（单位：秒）" prop="duration">
                        <el-input v-model="dataForm.duration" placeholder="调用耗时（单位：秒）"></el-input>
                </el-form-item>
                <el-form-item label="请求数据（JSON格式）" prop="requestData">
                        <el-input v-model="dataForm.requestData" placeholder="请求数据（JSON格式）"></el-input>
                </el-form-item>
                <el-form-item label="响应数据（JSON格式）" prop="responseData">
                        <el-input v-model="dataForm.responseData" placeholder="响应数据（JSON格式）"></el-input>
                </el-form-item>
                <el-form-item label="错误信息（如果调用失败）" prop="errorMessage">
                        <el-input v-model="dataForm.errorMessage" placeholder="错误信息（如果调用失败）"></el-input>
                </el-form-item>
                <el-form-item label="调用方用户代理信息" prop="userAgent">
                        <el-input v-model="dataForm.userAgent" placeholder="调用方用户代理信息"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            logId: null,
            interfaceId: null,
            callTime: null,
            statusCode: null,
            ipAddress: null,
            duration: null,
            requestData: null,
            responseData: null,
            errorMessage: null,
            userAgent: null
          },
          dataRule: {
            logId: [
                        { required: true, message: '调用记录ID，主键不能为空', trigger: 'blur' }
            ],
            interfaceId: [
                        { required: true, message: '接口ID，关联app_integration_interface表不能为空', trigger: 'blur' }
            ],
            callTime: [
                        { required: true, message: '调用时间不能为空', trigger: 'blur' }
            ],
            statusCode: [
                        { required: true, message: '响应状态码不能为空', trigger: 'blur' }
            ],
            ipAddress: [
                        { required: true, message: '调用方IP地址不能为空', trigger: 'blur' }
            ],
            duration: [
                        { required: true, message: '调用耗时（单位：秒）不能为空', trigger: 'blur' }
            ],
            requestData: [
                        { required: true, message: '请求数据（JSON格式）不能为空', trigger: 'blur' }
            ],
            responseData: [
                        { required: true, message: '响应数据（JSON格式）不能为空', trigger: 'blur' }
            ],
            errorMessage: [
                        { required: true, message: '错误信息（如果调用失败）不能为空', trigger: 'blur' }
            ],
            userAgent: [
                        { required: true, message: '调用方用户代理信息不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.logId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.logId = id || 0
            if (this.dataForm.logId) {
              this.$http({
                url: this.$http.adornUrl(`/app/interface_call_log/info/${this.dataForm.logId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appinterfacecalllog
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/interface_call_log/${!this.dataForm.logId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>